Imports BitSmoke.Core.Net.Presence
Imports BitSmoke.Core.Data
Imports BitSmoke.Core.Storage

Public Class frmMain
#Region " Private Properties "
    Private WithEvents zDataRoot As DataRoot = DataRoot.getDataRoot
    Private zPresence As New Presence(zDataRoot.Preferences.AnnounceFrequency, zDataRoot.Preferences.AnnouncePort, zDataRoot.MyProfile.GUID, zDataRoot.Preferences.ConnectionPort)
#End Region

#Region " Event Handlers "
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Text = Text & " - " & zDataRoot.MyProfile.GUID.ToString 'TODO: form text shows GUID for debug only

        'load form position and state
        If zDataRoot.Preferences.MainForm_Left > 0 And zDataRoot.Preferences.MainForm_Top > 0 Then
            Location = New Point(zDataRoot.Preferences.MainForm_Left, zDataRoot.Preferences.MainForm_Top)
        End If

        If zDataRoot.Preferences.MainForm_Width > 0 And zDataRoot.Preferences.MainForm_Height > 0 Then
            Size = New Size(zDataRoot.Preferences.MainForm_Width, zDataRoot.Preferences.MainForm_Height)
        End If

        WindowState = CType(zDataRoot.Preferences.MainForm_WindowState, FormWindowState)

        Visible = zDataRoot.Preferences.MainForm_Visible
    End Sub

    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
        If e.CloseReason = CloseReason.UserClosing Then
            e.Cancel = True

            Visible = False
        End If
    End Sub

    Private Sub frmMain_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles MyBase.FormClosed
        'save form position and state
        zDataRoot.Preferences.MainForm_Left = Location.X
        zDataRoot.Preferences.MainForm_Top = Location.Y
        zDataRoot.Preferences.MainForm_Width = Size.Width
        zDataRoot.Preferences.MainForm_Height = Size.Height
        zDataRoot.Preferences.MainForm_WindowState = WindowState
        zDataRoot.Preferences.MainForm_Visible = Visible

        'persist preferences, profiles etc.
        Store.save(zDataRoot)

        zPresence.Dispose()
        zPresence = Nothing

        zDataRoot.Dispose()
        zDataRoot = Nothing
    End Sub

    Private Sub mnuShowHide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuShowHide.Click
        Visible = Not Visible
    End Sub

    Private Sub mnuAbout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
        Dim frm As New frmAbout
        frm.ShowDialog(Me)
    End Sub

    Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
        Application.Exit()
    End Sub

    Private Sub NotifyIcon_Click(ByVal sender As Object, ByVal e As MouseEventArgs) Handles sysTray.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If Visible Then
                mnuShowHide.Text = "Hide"
            Else
                mnuShowHide.Text = "Show"
            End If
        End If
    End Sub

    Private Sub sysTray_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sysTray.MouseDoubleClick
        Visible = Not Visible
    End Sub
#End Region
End Class